/*******************************************************************************
 * OAUTH
 */
Ext.regController(App.controllers.OAUTH, {
			proxy:null,
			proxyId:'oauthProxy',
			creatProxy:function(){
				if(!this.proxy){
					var iframe = {
						id:this.proxyId,
						tag:'iframe',
						src:'about:blank',
						'class':'x-hide-display'
					};
					delete this.oauthProxy;
					this.proxy = Ext.DomHelper.append(Ext.getBody(),iframe);
				}
				return this.proxy;
			},
			open:function(url,opt){
				this.proxy.onload = this.createOAuthPage(this.getOAuthUrl(opt));
				this.proxy.src = url;
			},
			init:function(){
				if(Ext.isReady){
				 	this.creatProxy();
				}else{
					Ext.onReady(this.creatProxy,this);
				}
			},
			getOAuthCfg:function(type){
				return App.API[type.type || type].oauth
			},
			getOAuthUrl:function(type){
				return this.getOAuthCfg(type).action.authorize;
			},
			sign : function(options) {
				var oauth = this.getOAuthCfg(options),
				url = 'about:blank',
				consumerKey = oauth.secret.consumerKey,
				consumerSecret =  oauth.secret.consumerSecret,
				callback = oauth.callback,
				accessor = {
						consumerSecret:consumerSecret,
						tokenSecret:oauth.secret.token
					},
					message = {
						method:oauth.method,
						action:oauth.action.request,
						parameters:{
							oauth_callback:callback,
							oauth_consumer_key:consumerKey,
							oauth_nonce:OAuth.nonce(32),//获取随机数
							oauth_signature_method:oauth.signatureMethod,
							oauth_timestamp: OAuth.timestamp(),//获取时间戳
							oauth_version:oauth.version
						}
					};
					//oauth请求签 名
					OAuth.SignatureMethod.sign(message, accessor);
					var signcode = message.parameters['oauth_signature'].replace('+','%2B');
					url = message.action + '?' + OAuth.SignatureMethod.normalizeParameters(message.parameters)+ '&oauth_signature=' + signcode;   				
					this.open(url,options);
			},
			login:function(options){
				var oauth = this.getOAuthCfg(options),
				url = oauth.action.login
					+'?callback=' + oauth.callback
					+'&source=' + oauth.secret.consumerKey
					+'&_dc=' + Math.random();
				this.createOAuthPage(url)();
			},
			createOAuthPage : function(url) {
				var body = Ext.getBody();
				return function(){
			 		var oauthPage = window.open(url,"oauthPage","width="+body.getWidth()+",height="+body.getHeight()+",toolbar=no,menubar=no,resizable=no,status=no");
			 		oauthPage.focus();
			 		return;
				};
		 	}
		});